package com.fandou.base.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by yzj on 2019/6/12.
 */

public abstract class BasePlayerFragment extends BaseFragment {
	protected Handler handler;

	protected boolean isInit;

	protected View pauseCoverView;

	protected OnPlayerFragmentListener onPlayerFragmentListener;

	protected OnTouchEventListener onTouchEventListener;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler();
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		view.setOnClickListener(onOutsideClickListener);
		if (pauseCoverView != null) {
			pauseCoverView.setOnClickListener(onOutsideClickListener);
		}
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		isInit = true;
	}

	public void continuePlay() {
		if (isInit) {
			handler.post(this::play);
		}
	}

	public void pausePlay() {
		if (isInit) {
			handler.post(this::pause);
		}
	}

	public void stopPlay() {
		if (isInit) {
			handler.post(this::stop);
		}
	}

	protected abstract void play();
	protected abstract void pause();
	protected abstract void stop();

	public void showPauseCoverView() {
		handler.post(() -> {
			if (pauseCoverView != null) {
				pauseCoverView.setVisibility(View.VISIBLE);
			}
		});
	}

	protected View.OnClickListener onOutsideClickListener = v -> {
		if (onTouchEventListener != null) {
			onTouchEventListener.onTouch();
		}
	};

	public void setOnTouchEventListener(OnTouchEventListener onTouchEventListener) {
		this.onTouchEventListener = onTouchEventListener;
	}

	public interface OnTouchEventListener {
		void onTouch();
	}

	public interface OnPlayerFragmentListener extends OnFragmentListener {
		void onPlay();
		void onPause();
	}
}
